package day3.demo1;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/ant")
public class AntCtrl {
    /*
    localhost/ant/p1---无法访问
    ocalhost/ant/p1/---ok
    localhost/ant/p1/abc--ok
    localhost/ant/p1/abc/123--无法访问
    */
    @GetMapping("/p1/*")
    public String a1(){
        return "a1";
    }

    /*
    localhost/ant/p2/a/b---ok
    localhost/ant/p2/abc--ok
    localhost/ant/p2/--ok
    localhost/ant/p2--ok
    */
    @RequestMapping("/p2/**")
    public void a2(){
        System.out.println("p2");
    }

    /*
    http://localhost/ant/p3/ab--无法访问
    http://localhost/p1/m5/a---ok
    */
    @RequestMapping("/p3/?")
    public void a3(){
        System.out.println("p3");
    }


    //localhost/ant/p4/123/abc
    @RequestMapping("/p0")
    public String a0(String id, String n){
        System.out.println("a4,id="+id+",name="+n);
        return "abc";
    }

    //localhost/ant/p4/123/abc
    @RequestMapping("/p4/{id}/{name}")
    public String a4(@PathVariable String id, @PathVariable("name") String n){
        System.out.println("a4,id="+id+",name="+n);
        return "abc";
    }
    //localhost/ant/p5/123或localhost/ant/p5/123/
//用正则限定只有id为数字才能访问该资源
    @RequestMapping("/p5/{id:\\d+}")//用正则限定只有id为数字才能访问该资源
    public void a5(@PathVariable Integer id){
        System.out.println("a5,id="+id);
    }

    @RequestMapping(path={"/p6","/m6"})
    public void a6(){
        System.out.println("两种url访问同一资源");
    }
}